'use strict';
// 通过 require 引入 vk 实例
const vk = require('vk-unicloud');
// 通过 vk.init 初始化 vk实例（只有初始化后才能使用）
vk.init({
	baseDir: __dirname,
	requireFn: require
});
const db = uniCloud.database();
const _ = db.command;
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	var t = Math.floor(Date.now() / 1000) + 7200 //两个小时后的的时间戳单位（秒）
	var res_tv_services = event.res_tv_services
	for (var j = 0; j < res_tv_services.rows.length; j++) {

		let access_token = await vk.openapi.weixin.auth.getAccessToken({
			appid: "wx15c580155e3ec00e",
			appsecret: "6880e79e8fc057c8b7cdfa8cf127c21a"
		});
		let apiUrl =
			"https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token=" + access_token
		let update_res = await uniCloud.httpclient.request(apiUrl, {
			method: 'POST',
			data: {
				media_id: Number(res_tv_services.rows[j].media_id),
				t: t,
			},
			contentType: 'json', // 指定以application/json发送data内的数据
			dataType: 'json' // 指定返回值为json格式，自动进行parse
		})
		var tv_services_url = 0;
		if (update_res.data.errcode == 0) {
			tv_services_url = update_res.data.media_info.mp4_url;
			let newInfo = await vk.baseDao.updateById({
				dbName: "a_tv_series",
				id: res_tv_services.rows[j]._id,
				dataJson: {
					video_src: tv_services_url || 0
				},
			});
		}

	}
	//返回数据给客户端
	return event
};